package com.mall.generator;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.annotations.Mapper;

import javax.sql.DataSource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
/**
 *
 * 基于mybatis-plus的代码生成器
 *
 * @author zhaodc
 * @date 2021/10/15 15:09
 **/
public class CodeGeneratorApplication {
    public static void main(String[] args) {

        // 数据源配置
        HikariConfig hikariConfig = new HikariConfig();
        hikariConfig.setJdbcUrl("jdbc:mysql://47.117.69.119:3306/oauth2_server?useUnicode=true&allowMultiQueries=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai");
        hikariConfig.setDriverClassName("com.mysql.cj.jdbc.Driver");
        hikariConfig.setUsername("root");
        hikariConfig.setPassword("Aa1234567890...");
        hikariConfig.setConnectionTimeout(1000000);
        DataSource dataSource = new HikariDataSource(hikariConfig);
        DataSourceConfig.Builder datasourceBuilder = new DataSourceConfig.Builder(dataSource);
        // 代码生成器
        AutoGenerator autoGenerator = new AutoGenerator(datasourceBuilder.build());

        //1. 全局配置
        GlobalConfig.Builder globalConfigBuilder = new GlobalConfig.Builder();
        GlobalConfig globalConfig = globalConfigBuilder.author("zhao dong chao")
                .commentDate(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd mm:hh:ss")))
                .fileOverride()
                .dateType(DateType.ONLY_DATE)
                .openDir(true)
                .outputDir("E:\\tmp")
                .build();

        autoGenerator.global(globalConfig);

        // 包名策略配置
        PackageConfig.Builder packageConfigBuilder = new PackageConfig.Builder();
        packageConfigBuilder.parent("com.dong.auth.module")
                .moduleName("user")
                .controller("controller")
                .service("service")
                .serviceImpl("service.impl")
                .mapper("mapper")
                .entity("entity")
                .xml("dao")
                .build();

        autoGenerator.packageInfo(packageConfigBuilder.build());

        //策略
        StrategyConfig.Builder strategyConfigBuilder = new StrategyConfig.Builder();
        StrategyConfig strategyConfig = strategyConfigBuilder.addTablePrefix("t_")
                .enableSkipView()
                .enableCapitalMode()
                //controller 配置
                .controllerBuilder()
                .enableRestStyle()
                .enableHyphenStyle()
                .convertFileName(entityName -> convertClassName(entityName,"Controller"))
                //service配置
                .serviceBuilder()
                .convertServiceFileName(entityName -> convertClassName(entityName,"Service"))
                .convertServiceImplFileName(entityName -> convertClassName(entityName,"ServiceImpl"))
                .mapperBuilder()
                .convertMapperFileName(entityName -> convertClassName(entityName, "Mapper"))
                .convertXmlFileName(entityName -> convertClassName(entityName,"Mapper"))
                .entityBuilder()
                .convertFileName(entityName -> convertClassName(entityName,null))
                .columnNaming(NamingStrategy.underline_to_camel)
                .enableLombok()
                .enableSerialVersionUID()
                .idType(IdType.INPUT)
                .enableTableFieldAnnotation()
                .build();

        autoGenerator.strategy(strategyConfig);


        autoGenerator.execute();

    }
    private static String convertClassName(String entityName,String suffix){
        String[] entityNames = entityName.split("_");
        if (entityNames.length > 0) {
            StringBuilder fileName = new StringBuilder(entityNames[0]);
            for (int i = 1; i < entityNames.length; i++) {
                fileName.append(entityNames[i].substring(0, 1).toUpperCase())
                        .append(entityNames[i].substring(1));
            }
            if (null != suffix){
                fileName.append(suffix);
            }
            return fileName.toString();
        }
        return entityName;
    }
}
